// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2023-12-01 16:05:36
// 生成路径: internal/app/plugins/sms/logic/plugin_sms_log.go
// 生成人：gfast
// desc:短信日志
// company:云南奇讯科技有限公司
// ==========================================================================

package logic

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/sms/dao"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/sms/model"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/sms/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/sms/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterPluginSmsLog(New())
}

func New() service.IPluginSmsLog {
	return &sPluginSmsLog{}
}

type sPluginSmsLog struct{}

func (s *sPluginSmsLog) List(ctx context.Context, req *model.PluginSmsLogSearchReq) (listRes *model.PluginSmsLogSearchRes, err error) {
	listRes = new(model.PluginSmsLogSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.PluginSmsLog.Ctx(ctx).WithAll()
		if req.SmsType != "" {
			m = m.Where(dao.PluginSmsLog.Columns().SmsType+" = ?", req.SmsType)
		}
		if req.MsgType != "" {
			m = m.Where(dao.PluginSmsLog.Columns().MsgType+" = ?", req.MsgType)
		}
		if req.Templateid != "" {
			m = m.Where(dao.PluginSmsLog.Columns().Templateid+" = ?", req.Templateid)
		}
		if req.Mobiles != "" {
			m = m.Where(dao.PluginSmsLog.Columns().Mobiles+" = ?", req.Mobiles)
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.PluginSmsLog.Columns().CreatedAt+" >=? AND "+dao.PluginSmsLog.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id desc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.PluginSmsLogListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.PluginSmsLogListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.PluginSmsLogListRes{
				Id:         v.Id,
				SmsType:    v.SmsType,
				MsgType:    v.MsgType,
				Templateid: v.Templateid,
				Mobiles:    v.Mobiles,
				Params:     v.Params,
				CreatedAt:  v.CreatedAt,
			}
		}
	})
	return
}

func (s *sPluginSmsLog) GetById(ctx context.Context, id uint) (res *model.PluginSmsLogInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.PluginSmsLog.Ctx(ctx).WithAll().Where(dao.PluginSmsLog.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sPluginSmsLog) Add(ctx context.Context, req *model.PluginSmsLogAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.PluginSmsLog.Ctx(ctx).Insert(do.PluginSmsLog{
			SmsType:    req.SmsType,
			MsgType:    req.MsgType,
			Templateid: req.Templateid,
			Mobiles:    req.Mobiles,
			Params:     req.Params,
			Result:     req.Result,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sPluginSmsLog) Edit(ctx context.Context, req *model.PluginSmsLogEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.PluginSmsLog.Ctx(ctx).WherePri(req.Id).Update(do.PluginSmsLog{
			SmsType:    req.SmsType,
			MsgType:    req.MsgType,
			Templateid: req.Templateid,
			Mobiles:    req.Mobiles,
			Params:     req.Params,
			Result:     req.Result,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sPluginSmsLog) Delete(ctx context.Context, ids []uint) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.PluginSmsLog.Ctx(ctx).Delete(dao.PluginSmsLog.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
